package com.gcalsync.component;

import com.gcalsync.option.Options;
import com.gcalsync.store.Store;

import javax.microedition.lcdui.*;

/**
 * Set options whether to upload events, download events, or both.
 *
 * @author Thomas Oldervoll, thomas@zenior.no
 * @author $Author: thomasold $
 * @version $Rev: 12 $
 * @date $Date: 2006-09-16 19:52:29 -0400 (Sat, 16 Sep 2006) $
 */
public class UploadDownloadComponent extends MVCComponent {

    private Form form;
    private ChoiceGroup choices;
    private Command saveCommand;

    public Displayable getDisplayable() {
        return form;
    }

    protected void initModel() throws Exception {
    }

    protected void createView() throws Exception {
        form = new Form("Upload/download events");
        choices = new ChoiceGroup("", ChoiceGroup.MULTIPLE);
        choices.append("Upload", null);
        choices.append("Download", null);
        form.append(choices);

        saveCommand = new Command("Save", Command.OK, 1);
        form.addCommand(saveCommand);

        Command cancelCommand = new Command("Cancel", Command.CANCEL, 2);
        form.addCommand(cancelCommand);

        form.setCommandListener(this);
        updateView();
    }

    protected void updateView() throws Exception {
        Options options = Store.getOptions();
        choices.setSelectedIndex(0, options.upload);
        choices.setSelectedIndex(1, options.download);
    }

    public void commandAction(Command command, Displayable displayable) {
        if (command.getLabel().equals(saveCommand.getLabel())) {
            Options options = Store.getOptions();
            options.upload = choices.isSelected(0);
            options.download = choices.isSelected(1);
            Store.saveOptions();
        }
        Components.options.showScreen();
    }
}
